package common.manager
{
	/**
	 *
	 <Avatar>
	<Item id="0" scene="SceneA" statu = "walk" className="Pand" swfURL="Resources/Avatar/Pand.swf" x="400" y="250" activeRange="0,0,1600,1000"/>
	</Avatar> 
	 * 
	 * id           化身的Id
	 * scene        出现的场景
	 * statu        初始化的状态与swf中的元件帧标签对应
	 * className    swf中的化身导出类名
	 * swfURL       swf路径
	 * x			初始化的x位置
	 * y            初始化的y位置
	 * activeRang   活动矩形范围
	 * */	
	import common.avatar.AvatarSprite;
	import common.manager.Resource;
	import common.world.World;
	
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;
	
	
	public class Avatar
	{
		private var m_data : Object;
		private var m_showBodyRange:Boolean;  //显示化身身体范围
		
		public function Avatar()
		{
			
		}
		
		public function build(classInfo:Object) : void
		{
			m_data = new Object;
			
			var xml : XML = Resource.getXML("Avatar");
			
			for each(var item:XML in xml.elements("Item"))
			{
				var info : Object = new Object;
				
				info["id"] = item.@id.toString();
				info["scene"] = item.@scene.toString();
				info["statu"] = item.@statu.toString();
				info["className"] = item.@className.toString();
				info["swfURL"] = item.@swfURL.toString();
				info["x"] = Number(item.@x);
				info["y"] = Number(item.@y);
				
				var rangeStr : String = item.@activeRange.toString();
				var rangePos : Array = rangeStr.split(",");
				
				info["activeRange"] = new Rectangle(rangePos[0],rangePos[1],rangePos[2],rangePos[3]);
				
				rangeStr = item.@bodyRange.toString();
				rangePos = rangeStr.split(",");
				
				info["bodyRange"] = new Rectangle(rangePos[0],rangePos[1],rangePos[2],rangePos[3]);
				
				var cs : Class = classInfo[info["className"]];
				var avatar : AvatarSprite = new cs;
				
				avatar.x = info["x"];
				avatar.y = info["y"];
				avatar.bodyRange = info["bodyRange"];
				avatar.activeRange = info["activeRange"];
				avatar.curStatu = info["statu"];
				avatar.build(info["className"],info["swfURL"]);
				avatar.showBodyRange = true;
				avatar.ID = info["id"];
				
				World.addEntity(avatar.ID,avatar);
			}
		}
		
		/**
		 *  设置是否可见身体范围矩形
		 */		
		public function set showBodyRange(view : Boolean) : void
		{
			m_showBodyRange = view;
			
			for each(var avatar:AvatarSprite in m_data)
			{
				avatar.showBodyRange = view;
			}
		}
		
		/**
		 * 获取是否显示身体矩形范围
		 * @return  
		 */		
		public function get showBoyRange() : Boolean
		{
			return m_showBodyRange;
		}
		
		public function hitTest() : void
		{
			
		}
		
		private function data() : Object
		{
			return m_data;
		}
	}
}